package main

import (
	"bufio"
	"fmt"
	"go-guide/network/im/server"
	"net"
	"os"
	"strings"
)

func main() {
	add := ":1234"
	reader := bufio.NewReader(os.Stdin)

	server.NewServer(add, func(conn *net.TCPConn) {
		go func() {
			for {
				data := make([]byte, 1024)
				i, e := conn.Read(data)
				if e != nil {
					fmt.Println("")
				}
				content := string(data[:i])
				fmt.Printf("【client】-> %s\n", content)
			}
		}()
		for {
			fmt.Print("【provider】-> ")

			text, _ := reader.ReadString('\n')
			text = strings.Replace(text, "\n", "", -1)

			_, err := conn.Write([]byte(text))
			if err != nil {
				fmt.Printf("send message failed, err = %v \n", err)
			}
			//fmt.Printf("send client[%s] message success, len = %d \n", add, n)
		}
	})

}
